package com.example.l15_yanweicheng;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MusicActivity extends AppCompatActivity{

    BroadcastReceiver broadcastReceiver;
    MusicService.LocalBinder localBinder;
    Button button1,button2,button3,button4,button5,button6;
    boolean isConnected = false;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            localBinder = (MusicService.LocalBinder)service;
            isConnected = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isConnected = false;
        }
    };

    private void stopAndUnbindService(){
        if (isConnected){
            unbindService(connection);
            Intent serviceIntent = new Intent(MusicActivity.this, MusicService.class);
            stopService(serviceIntent);
            isConnected = false;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        button1 = findViewById(R.id.button);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        button5 = findViewById(R.id.button5);
        button6 = findViewById(R.id.button6);

        button1.setOnClickListener(v -> {
            if (!isConnected){
                Intent serviceIntent = new Intent(MusicActivity.this, MusicService.class);
                startService(serviceIntent);
                bindService(serviceIntent,connection, Context.BIND_AUTO_CREATE);
            }
        });
        button2.setOnClickListener(v -> {
            if (isConnected){
                localBinder.startMediaPlayer();
            }
        });
        button3.setOnClickListener(v -> {
            if (isConnected){
                localBinder.pauseMediaPlayer();
            }
        });;
        button4.setOnClickListener(v -> {
            if (isConnected){
                localBinder.forwardMediaPlayer();
            }
        });;
        button5.setOnClickListener(v -> {
            if (isConnected){
                localBinder.stopMediaPlayer();
            }
        });;
        button6.setOnClickListener(v -> {
            if (isConnected){
                stopAndUnbindService();
            }
        });

        broadcastReceiver = new MyBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.l15_yanweicheng.control_music");
        registerReceiver(broadcastReceiver,intentFilter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        if (item.getItemId() == android.R.id.home){
            finish();
            return true;
        } else {
            return false;
        }
    }
    private class MyBroadcastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String control_music = intent.getStringExtra("control_music");
            switch (control_music){
                case "start_music":
                    localBinder.startMediaPlayer();
                    break;
                case "pause_music":
                    localBinder.pauseMediaPlayer();
                    break;
                case "forward_music":
                    localBinder.forwardMediaPlayer();
                    break;
                case "stop_music":
                    localBinder.stopMediaPlayer();
                    break;
                case "close_music":
                    stopAndUnbindService();
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopAndUnbindService();
        unregisterReceiver(broadcastReceiver);
    }

}

